﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Model.UI;
using CoreShop.PrintService;
using CoreShop.Services;
using MySqlX.XDevAPI;
using SqlSugar;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace CoreShop.App
{
    public partial class frmPrintScheme : UIForm
    {
        int chemeId = 0;
        private UIDishSchemeDetailItem uiDetailSelected = null;
        public frmPrintScheme()
        {
            InitializeComponent();
        }

        private void LoadPrintScheme()
        {
            chemeId = 0;
            tvScheme.Nodes.Clear();
            var where = Expressionable.Create<corecmsprintscheme>();
            where = where.And(p => p.id > 0);

            //如果不启用厨房打印，不显示厨房打印方案名称
            if (!AppConstVars.systemsetup.isKitchen)
                where = where.And(p => p.printerType == (int)GlobalEnumVars.printerType.PosBill);

            corecmsprintscheme printscheme = null;

            var lstSource = new corecmsprintschemeServices().AsQueryable().Where(where.ToExpression()).ToList();
            foreach (var item in lstSource)
            {
                if (chemeId == 0)
                {
                    chemeId = item.id;
                    printscheme = item;
                }                    
                string sName = string.Format("{0}<{1}>", item.printerName, item.gearName);
                TreeNode treeNode10 = new TreeNode(sName);
                treeNode10.Tag = item.id.ToString();
                tvScheme.Nodes.Add(treeNode10);
            }
            if (tvScheme.Nodes.Count > 0)
            {
                tvScheme.Nodes[0].Expand();
                GetSchemeDetail(printscheme);
            }
        }

        private void FDishPinterSetup_Shown(object sender, System.EventArgs e)
        {
        }

        private void GetSchemeDetail(corecmsprintscheme printscheme)
        {
            if (printscheme == null)
            {
                UIMessageBox.Show("没有找到相应的打印机方案", "系统出错:", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }   

            var where = Expressionable.Create<corecmsprintbilltype>();
            where = where.And(a => a.id > 0);
            var corecmsprintbilltypes = new corecmsprintbilltypeServices().AsQueryable().Where(where.ToExpression()).OrderBy(p=>p.id).ToList();
            var schemeDetails = new corecmsprintschemedetailServices().QueryByPrintSchemeId(printscheme.id);
            laySchemeDetail.Visible = false;
            laySchemeDetail.Clear();
            foreach (var info in corecmsprintbilltypes)
            {
                info.printSchemeId = printscheme.id;
                var schemeDetail = schemeDetails.Find(p => p.billtypeId == info.id && p.selectedFlag == true);
                if (schemeDetail != null)
                {
                    info.selectedFlag = schemeDetail.selectedFlag;
                    info.printCount = schemeDetail.printCount.ToString();
                    info.templateName = schemeDetail.templateName;
                    info.foodPrintTypeName = EnumHelper.GetDescByValue<GlobalEnumVars.foodPrintType>(schemeDetail.foodPrintType);
                    info.foodSortName = EnumHelper.GetDescByValue<GlobalEnumVars.foodSort>(schemeDetail.foodSort);
                    info.foodSort = schemeDetail.foodSort;
                    info.templateId = schemeDetail.templateId;

                    UIDishSchemeDetailItem item = new UIDishSchemeDetailItem(info);
                    item.CheckBoxChecked += Detail_OnCheckBoxChecked;
                    item.ModifyItemClick += Detail_OnModifyItemClick;
                    item.Click += DetailItem_Click;
                    laySchemeDetail.Add(item);
                }
            }
            laySchemeDetail.Visible = true;
        }

        private void DetailItem_Click(object sender, System.EventArgs e)
        {
            uiDetailSelected = (UIDishSchemeDetailItem)sender;
            int id = CommFunc.ConvertToInt32(uiDetailSelected.Tag);

        }

        private void Detail_OnModifyItemClick(object sender, System.EventArgs e)
        {
            try
            {
                corecmsprintbilltype item = (corecmsprintbilltype)sender;

                //打印方案
                var printscheme = new corecmsprintschemeServices().SelectById(item.printSchemeId);
                if (printscheme == null)
                {
                    UIMessageBox.Show("没有找到相应的打印机方案", "系统出错:", UIStyle.Orange, UIMessageBoxButtons.OK);
                    return;
                }
                var frm = new frmPrintSchemeDetailInfo(item, printscheme);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    GetSchemeDetail(printscheme);
                }
                frm.Dispose();
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message, "系统出错:", UIStyle.Orange, UIMessageBoxButtons.OK);
            }
        }
               
        private void Detail_OnCheckBoxChecked(object sender, System.EventArgs e)
        {

        }

        private void btnAddPrintScheme_Click(object sender, System.EventArgs e)
        {
            var printscheme = new corecmsprintscheme() 
            { 
                printerType = (int)GlobalEnumVars.printerType.Kitchen,                
                sendPrintWay = (int)GlobalEnumVars.SendPrintWay.Command
            };
            var frm = new frmPrintSchemeInfo(printscheme);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                LoadPrintScheme();
            }
            frm.Dispose();
        }

        private void frmPrintScheme_Load(object sender, System.EventArgs e)
        {
            LoadPrintScheme();
            tvScheme.ExpandAll();
        }

        private void disHeader_CheckBoxChecked(object sender, System.EventArgs e)
        {
            var items = laySchemeDetail.GetControls<UIDishSchemeDetailItem>(true);
            foreach (var item in items)
            {
                item.Checked = disHeader.Checked;
            }
        }

        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void tvScheme_AfterSelect(object sender, TreeViewEventArgs e)
        {
            chemeId = CommFunc.ConvertToInt32(e.Node.Tag);
            corecmsprintscheme cheme = new corecmsprintschemeServices().SelectById(chemeId);
            GetSchemeDetail(cheme);
        }

        private void btnEditPrintScheme_Click(object sender, EventArgs e)
        {
            if (chemeId < 1) { UIMessageBox.Show("请选择需要修改的打印机", "系统提示", UIStyle.Black, UIMessageBoxButtons.OK); return; }
            corecmsprintscheme cheme = new corecmsprintschemeServices().SelectById(chemeId);
            frmPrintSchemeInfo frm = new frmPrintSchemeInfo(cheme);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                LoadPrintScheme();
            }
            frm.Dispose();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (chemeId < 1) { UIMessageBox.Show("请选择需要删除的打印机", "系统提示", UIStyle.Black, UIMessageBoxButtons.OK); return; }

            var printschemes =  new corecmsprintschemeServices().GetList();
            if (printschemes.Count <= 1)
            {
                UIMessageBox.Show("必须保留一条打印机数据。", "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                return;
            }

            if (!ShowAskDialog("确定要删除当前所选择的打印机吗？")) return;
            var bl = new corecmsprintschemeServices().Delete(p => p.id == chemeId);
            if (!bl)
            {
                UIMessageBox.Show("提交保存失败。", "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                return;
            }
            bl = new corecmsprintdeskcategoryServices().Delete(p => p.printSchemeId == chemeId);
            bl = new corecmsprintschemedetailServices().Delete(p => p.printSchemeId == chemeId);
            bl = new corecmsfoodprintschemeServices().Delete(p => p.printSchemeId == chemeId);
            UIMessageBox.Show("删除完成", "提示", UIStyle.Green, UIMessageBoxButtons.OK);
            LoadPrintScheme();
        }

        private void btnAddPrintScheme0_Click(object sender, EventArgs e)
        {
            var printscheme = new corecmsprintscheme()
            {
                printerType = (int)GlobalEnumVars.printerType.PosBill,
                sendPrintWay = (int)GlobalEnumVars.SendPrintWay.Drive
            };
            var frm = new frmPrintSchemeInfo(printscheme);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                LoadPrintScheme();
            }
            frm.Dispose();
        }

        private void btnPrintTemplate_Click(object sender, EventArgs e)
        {
            if (!ProcessUtil.GetUserRole(GlobalEnumVars.UserRoleType.isSysSetup, 0)) return;
            var frm = new frmPrintTemplate();
            frm.ShowDialog();
            frm.Dispose();
        }
    }
}
